package cn.darkhorse.mesadmin.controller.system;

import cn.darkhorse.messystem.service.IMesSysPermsService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysMenuService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;


/**
 *
 * @author yuanjun
 * @since 2022/11/11 13:52
 */
@RestController
@RequestMapping("/mes/menu")
public class MesPermissionController extends BaseController {

    @Resource
    private ISysMenuService sysMenuService;
    @Resource
    private IMesSysPermsService iMesSysPermsService;

    /**
     * 判断当前用户是否有某个权限
     * @param perms 权限标识
     * @return
     */
    @PostMapping("/allow")
    public AjaxResult getUsers(String perms) {
        Set<String> set = sysMenuService.selectMenuPermsByUserId(SecurityUtils.getUserId());
        if (SysUser.isAdmin(SecurityUtils.getUserId())){
            return AjaxResult.success(true);
        }
        return AjaxResult.success(set.contains(perms));
    }

    @RequestMapping(value = "/getuser-by-perms",method = RequestMethod.POST)
    public AjaxResult getUserByPerm(String perms){
        List<SysUser> users=iMesSysPermsService.getUsersByPerms(perms);
        return  AjaxResult.success(users);
    }

    /**
     * 判断当前用户是否有固定权限
     * @param permsList 权限标识
     * @return
     */
    @PostMapping("/getPersms")
    public AjaxResult getUsersPerms(String[] permsList) {
        Set<String> set = sysMenuService.selectMenuPermsByUserId(SecurityUtils.getUserId());
        List<String> hasPerms=new ArrayList<>();
        for (String s : permsList) {
            if(set.contains(s)){
                hasPerms.add(s);
            }
        }
        return AjaxResult.success(hasPerms);
    }
}
